package com.neu.cpspgateway.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateAndTimeUtil {
    public static void main(String[] args) {
        getWeekDate();
    }

    //获取当前日期
    public static String getDate(){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(now);
    }

    // 获取当前时间
    public static String getTime() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        return dateFormat.format(now);
    }

    //获取当前时间所在周的周一日期
    public static String getWeekDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if(dayWeek==1){
            dayWeek = 8;
        }
        System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);
        System.out.println("所在周星期一的日期：" + weekBegin);
        return weekBegin;
    }

    /*
    固液分离设备运行表
    白班：
    开、关机时间1，开机时间2   06:00:00-12:00:00
    夜班：
    关机时间2  12:00:00-24:00:00
     */
    public static String getShiftLabel(String time) throws ParseException {
        String dateFormat = "HH:mm:ss";
        Date nowTime = new SimpleDateFormat(dateFormat).parse(time);
        Date startTime = new SimpleDateFormat(dateFormat).parse("00:00:00");
        Date endTime = new SimpleDateFormat(dateFormat).parse("12:00:00");
        if (isEffectiveTime(nowTime, startTime, endTime))
            return  "白班";
        return "夜班";
    }

    public static String getOffTimeLabel(String shift, String time) throws Exception {
        String dateFormat = "HH:mm:ss";
        Date nowTime = new SimpleDateFormat(dateFormat).parse(time);
        String timeLabel = new String();
        if (shift.equals("白班")){
            Date startTime1 = new SimpleDateFormat(dateFormat).parse("12:00:00");
            Date endTime1 = new SimpleDateFormat(dateFormat).parse("24:00:00");
            if (isEffectiveTime(nowTime, startTime1, endTime1) == true) timeLabel = "offTime1";

        }else if (shift.equals("晚班")){
            Date startTime2 = new SimpleDateFormat(dateFormat).parse("18:00:00");
            Date endTime2 = new SimpleDateFormat(dateFormat).parse("24:00:00");
            if (isEffectiveTime(nowTime, startTime2, endTime2) == true) timeLabel = "offTime2";
        }
        return timeLabel;
    }
    /*
    固液分离设备运行表
    白班：
    开、关机时间1   8:00:00-11:59:59
    开、关机时间2  12:00:00-17:59:59
    夜班：
    开、关机时间1  18:00:00-23:59:59
    开、关机时间2  00:00:00-7:59:59
     */
//    public static String getOnTimeLabel(String shift, String time) throws Exception {
//        String dateFormat = "HH:mm:ss";
//        Date nowTime = new SimpleDateFormat(dateFormat).parse(time);
//        String timeLabel = new String();
//        if (shift.equals("白班")){
//            Date startTime1 = new SimpleDateFormat(dateFormat).parse("8:00:00");
//            Date endTime1 = new SimpleDateFormat(dateFormat).parse("11:59:59");
//            Date startTime2 = new SimpleDateFormat(dateFormat).parse("12:00:00");
//            Date endTime2 = new SimpleDateFormat(dateFormat).parse("17:59:59");
//            if (isEffectiveTime(nowTime, startTime1, endTime1) == true) timeLabel = "onTime1";
//            if (isEffectiveTime(nowTime, startTime2, endTime2) == true) timeLabel = "onTime2";
//
//        }else if (shift.equals("晚班")){
//            Date startTime1 = new SimpleDateFormat(dateFormat).parse("18:00:00");
//            Date endTime1 = new SimpleDateFormat(dateFormat).parse("23:59:59");
//            Date startTime2 = new SimpleDateFormat(dateFormat).parse("00:00:00");
//            Date endTime2 = new SimpleDateFormat(dateFormat).parse("7:59:59");
//            if (isEffectiveTime(nowTime, startTime1, endTime1) == true) timeLabel = "onTime1";
//            if (isEffectiveTime(nowTime, startTime2, endTime2) == true) timeLabel = "onTime2";
//        }
//        return timeLabel;
//    }
//
//    public static String getOffTimeLabel(String shift, String time) throws Exception {
//        String dateFormat = "HH:mm:ss";
//        Date nowTime = new SimpleDateFormat(dateFormat).parse(time);
//        String timeLabel = new String();
//        if (shift.equals("白班")){
//            Date startTime1 = new SimpleDateFormat(dateFormat).parse("8:00:00");
//            Date endTime1 = new SimpleDateFormat(dateFormat).parse("11:59:59");
//            Date startTime2 = new SimpleDateFormat(dateFormat).parse("12:00:00");
//            Date endTime2 = new SimpleDateFormat(dateFormat).parse("17:59:59");
//            if (isEffectiveTime(nowTime, startTime1, endTime1) == true) timeLabel = "offTime1";
//            if (isEffectiveTime(nowTime, startTime2, endTime2) == true) timeLabel = "offTime2";
//
//        }else if (shift.equals("晚班")){
//            Date startTime1 = new SimpleDateFormat(dateFormat).parse("18:00:00");
//            Date endTime1 = new SimpleDateFormat(dateFormat).parse("23:59:59");
//            Date startTime2 = new SimpleDateFormat(dateFormat).parse("00:00:00");
//            Date endTime2 = new SimpleDateFormat(dateFormat).parse("7:59:59");
//            if (isEffectiveTime(nowTime, startTime1, endTime1) == true) timeLabel = "offTime1";
//            if (isEffectiveTime(nowTime, startTime2, endTime2) == true) timeLabel = "offTime2";
//        }
//        return timeLabel;
//    }

    //判断当前时间是否在时间区间内
    public static boolean isEffectiveTime(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        return nowTime.after(startTime) && nowTime.before(endTime);
    }

    private DateAndTimeUtil() { }
}
